package com.amigo.online.provider.manager.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.amigo.online.provider.manager.mapper.VideoMapper;
import com.amigo.online.provider.manager.mapper.VideoRecommendMapper;
import com.amigo.online.provider.manager.model.entity.Video;
import com.amigo.online.provider.manager.model.entity.VideoRecommend;
import com.amigo.online.provider.manager.service.VideoRecommendService;

@Service
public class VideoRecommendServiceImpl implements VideoRecommendService {

    @Resource
    private VideoRecommendMapper videoRecommendMapper;

    @Resource
    private VideoMapper videoMapper;

    @Override
    @Transactional
    public boolean saveVideoRecommend(VideoRecommend videoRecommend) {
        videoRecommend.setCreatedTime(System.currentTimeMillis() / 1000);
        videoRecommend.setChecked(1);
        Video vo = new Video();
        vo.setUpdatedTime(System.currentTimeMillis() / 1000);
        vo.setId(videoRecommend.getVideoId().longValue());
        vo.setWeight(videoMapper.getVideoMaximalWeight() + 0.05);
        // 修改视频表里的recommend字段
        int i = videoMapper.updateVideo(vo);
        // 添加到热门表
        int ii = videoRecommendMapper.insertVideoRecommended(videoRecommend);
        if (i <= 0 || ii <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public List<VideoRecommend> findVideoRecommended(VideoRecommend vrd) {
        return videoRecommendMapper.findVideoRecommended(vrd);
    }

    @Override
    public boolean updateVideoRecommend(VideoRecommend vrd) {
        vrd.setCreatedTime(System.currentTimeMillis());
        vrd.setUpdatedTime(System.currentTimeMillis());
        int i = videoRecommendMapper.updateVideoRecommend(vrd);
        if (i <= 0) {
            return false;
        } else {
            return true;
        }

    }

    @Override
    public boolean deleteVideoRecommend(VideoRecommend vrd) {
        int i = videoRecommendMapper.deleteVideoRecommend(vrd);
        if (i <= 0) {
            return false;
        } else {
            return true;
        }
    }

}
